package com.atguigu.lease.admin.controller.apartment;

import com.atguigu.lease.admin.service.ApartmentInfoService;
import com.atguigu.lease.model.bean.ApartmentInfo;
import com.atguigu.lease.model.dto.ApartmentItemQueryDto;
import com.atguigu.lease.model.dto.ApartmentSubmitDto;
import com.atguigu.lease.model.result.Response;
import com.atguigu.lease.model.vo.ApartmentDetailVo;
import com.atguigu.lease.model.vo.ApartmentItemVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name="公寓信息管理模块")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentInfoController {

    @Autowired
    ApartmentInfoService apartmentInfoService;

    @Operation(summary = "6、根据区县id查询公寓列表")
    @GetMapping("listInfoByDistrictId")
    public Response<List<ApartmentInfo>> listInfoByDistrictId(
            @RequestParam("id") Long districtId
    ){
        List<ApartmentInfo> apartmentInfos = apartmentInfoService.list(Wrappers.lambdaQuery(ApartmentInfo.class)
                .eq(ApartmentInfo::getDistrictId, districtId));
        return Response.ok(apartmentInfos);
    }

    @Operation(summary = "5、根据id更新公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Response updateReleaseStatusById(
            @RequestParam("id")Long id,
            @RequestParam("status")String status
    ) {
        apartmentInfoService
                .update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                        .set(ApartmentInfo::getIsRelease, status)
                        .eq(ApartmentInfo::getId, id));
        return Response.ok(null);
    }
    @Operation(summary = "4、根据id查询公寓详情")
    @GetMapping("getDetailById")
    public Response<ApartmentDetailVo> getDetailById(@RequestParam("id") Integer id) {
        ApartmentDetailVo vo = apartmentInfoService.getDetailById(id);
        return Response.ok(vo);
    }
    @Operation(summary = "3、根据id删除公寓")
    @DeleteMapping("removeById")
    public Response removeById(@RequestParam("id") Long id) {
        apartmentInfoService.removeApartmentInfoById(id);
        return Response.ok(null);
    }
    @Operation(summary = "2、新增或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Response saveOrUpdate(@RequestBody ApartmentSubmitDto apartmentSubmitDto) {
        //因为保存的数据 包含多张表的数据，所以需要自定义方法
        apartmentInfoService.saveOrUpdateApartmentSubmitDto(apartmentSubmitDto);
        return Response.ok(null);
    }


    @Operation(summary = "1、根据条件分页查询公寓列表")
    @GetMapping("/pageItem")
    // pojo入参 表示接受请求参数，不能加注解！！
    public Response<Page<ApartmentItemVo>> pageItem(ApartmentItemQueryDto queryDto) {
        //数据涉及多表联查  需要自定义sql实现(通用service 和通用mapper 生成的都是单表CRUD的方法)
        Page<ApartmentItemVo> page =  apartmentInfoService.getPageItem(queryDto);
        return Response.ok(page);
    }
}
